package yt.kpool.activity.home;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.PoolCoinBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.DisplayUtil;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.Util;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.pop.CalculatorAcPop;

/**
 * 计算器
 */
public class CalculatorAc  extends AppCompatActivity {
    @BindView(R.id.tv_sy)
    TextView tv_sy;//收益
    @BindView(R.id.tv_sl)
    TextView tv_sl;//算力
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_price2)
    TextView tv_price2;
    @BindView(R.id.tv_d)
    TextView tv_d;
    @BindView(R.id.tv_s)
    TextView tv_s;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.ly_coin)
    LinearLayout ly_coin;
    @BindView(R.id.iv_icon)
    ImageView iv_icon;

    @BindView(R.id.scrollview)
    ScrollView scrollview;
    private int kind=1;
    private int Coin_kind=1;
    private LoadingDialog dialog;
    boolean b=true;
    private List<PoolCoinBean.DataBean.CoinListBean> listData;
    private CalculatorAcPop calculatorAcPop;
    private String str1,str2,Exchange,e1,ss1,s1,s2;
    private int number=0;
    private boolean isclick=true;
    private SPUtil spUtil;

    /**
     * 页面跳转
     *
     * @param
     */
    public static void start(Context mactivity, String position) {
        Intent intent = new Intent(mactivity, CalculatorAc.class);
        intent.putExtra("position", position);
        mactivity.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calcuator);
        ButterKnife.bind(this);
        initTitle();

        if(NetWorkUtil.checkNet(CalculatorAc.this))
        {
            getNetData();
        }
        else
        {

            ToastUtil.showToast(CalculatorAc.this,getResources().getString(R.string.internet_error));
        }

    }

    /**
     * 获取首页网络数据
     */
    private void getNetData()
    {
        dialog.show();
        NetHelperNew.PoolCoinList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                PoolCoinBean poolCoinBean = MyGson.getInstance().fromJson(data, PoolCoinBean.class);
                Exchange=poolCoinBean.getData().getExchange()+"";
                if(poolCoinBean.getType()!=1)
                {
                    Toast.makeText(CalculatorAc.this,poolCoinBean.getContent(),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    listData=poolCoinBean.getData().getCoinList();
                    if(listData.size()>0)
                    {
                        calculatorAcPop=new CalculatorAcPop(CalculatorAc.this,listData, new CalculatorAcPop.OnSelectItemListener(){
                            @Override
                            public void select(int i) {
                                number=i;
                                GlideUtil.loadImage(CalculatorAc.this, NetHelperNew.URL + listData.get(i).getIcon(), iv_icon, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
                                if(kind==1)
                                {

                                    setData(i);
                                }
                                else
                                {
                                    setData2(i);
                                }

                            }
                        });
                        setData(Integer.valueOf(getIntent().getStringExtra("position")));

                        kind=2;
                        tv_sy.setText("有效算力");
                        tv_sl.setText("收益");
                        tv_price.setVisibility(View.INVISIBLE);
                        tv_price2.setVisibility(View.VISIBLE);
                        tv_d.setText(str1);
                        tv_s.setText(str2);
                        edit.setText(ss1);
                        tv_content.setText(e1);

                        if(Coin_kind==1)
                        {

                            tv_price2.setText("≈ ¥ "+s2);
                        }

                        else
                        {

                            tv_price2.setText("≈ $ "+s2);
                        }
                        b=false;

                    }


                }

                dialog.dismiss();

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(CalculatorAc.this,msg);
            }
        });

    }

    /**
     * 设置计算器的数据
     * @param i
     */
    private void setData(int i)
    {

        GlideUtil.loadImage(CalculatorAc.this, NetHelperNew.URL + listData.get(i).getIcon(), iv_icon, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        tv_name.setText(listData.get(i).getCode().toUpperCase());
        str1=listData.get(i).getThroughputUnit()+"/s";
        str2=listData.get(i).getCode().toUpperCase()+"/天";
        tv_d.setText(str2);
        tv_s.setText(str1);
        e1=Util.GetNumber(listData.get(i).getThroughput()+"");
        ss1=Util.Division(listData.get(i).getThroughput()+"",listData.get(i).getThroughput()+"");
        edit.setText(e1);
        tv_content.setText(ss1);
        if(Coin_kind==1)
        {
            s1=Util.NMultiplication(listData.get(i).getPrice()+"",Exchange);
            s2=Util.Multiplication(s1,listData.get(i).getThroughput()+"");
            tv_price.setText("≈ ¥ "+s2);
        }

        else
        {
            s2=Util.Multiplication(listData.get(i).getPrice()+"",listData.get(i).getThroughput()+"");
            tv_price.setText("≈ $ "+s2);
        }
    }
    private void setData2(int i)
    {

        GlideUtil.loadImage(CalculatorAc.this, NetHelperNew.URL + listData.get(i).getIcon(), iv_icon, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        tv_name.setText(listData.get(i).getCode().toUpperCase());
        str1=listData.get(i).getThroughputUnit()+"/s";
        str2=listData.get(i).getCode().toUpperCase()+"/天";
        tv_d.setText(str2);
        tv_s.setText(str1);
        e1=Util.GetNumber(listData.get(i).getThroughput()+"");
        ss1=Util.Division(listData.get(i).getThroughput()+"",listData.get(i).getThroughput()+"");
        edit.setText(ss1);
        tv_content.setText(e1);
        if(Coin_kind==1)
        {
            s1=Util.NMultiplication(listData.get(i).getPrice()+"",Exchange);
            s2=Util.Multiplication(s1,listData.get(i).getThroughput()+"");
            tv_price2.setText("≈ ¥ "+s2);
        }

        else
        {
            s2=Util.Multiplication(listData.get(i).getPrice()+"",listData.get(i).getThroughput()+"");
            tv_price2.setText("≈ $ "+s2);
        }

    }

    /**
     * 设置标题栏
     */
    @SuppressLint("ClickableViewAccessibility")
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("计算器");
        if (spUtil == null)
            spUtil = new SPUtil(this, "fileName");
        if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
        {

            Coin_kind=1;
        }
        else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
        {
            Coin_kind=2;
        }
        dialog = new LoadingDialog(this, 0);


        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                if(kind==1)
                {
                    if(s.toString().equals("")||Double.valueOf(s.toString())==0)
                    {

                        ss1="0";
                        e1="0";
                        s2="0";
                        tv_content.setText("0");
                        tv_price.setText("≈ ¥ 0");
                    }
                    else
                    {
                        ss1=Double.valueOf(s.toString())/listData.get(number).getThroughput()+"";

                        tv_content.setText(Util.GetTNumber(ss1));

                        if(Coin_kind==1)
                        {
                            s1=Util.NMultiplication(listData.get(number).getPrice()+"",Exchange);
                            s2=Util.Multiplication(s1,s.toString());
                            tv_price.setText("≈ ¥ "+s2);
                        }

                        else
                        {
                            s2=Util.Multiplication(listData.get(number).getPrice()+"",s.toString());
                            tv_price.setText("≈ $ "+s2);
                        }
                    }


                }
                else
                {
                    if(s.toString().equals("")||Double.valueOf(s.toString())==0)
                    {

                        ss1="0";
                        e1="0";
                        s2="0";
                        tv_content.setText("0");
                        tv_price2.setText("≈ ¥ 0");
                    }
                    else
                    {
                        e1=Util.NMultiplication(s.toString(),listData.get(number).getThroughput()+"");
                        tv_content.setText(e1);
                        if(Coin_kind==1)
                        {
                            s1=Util.NMultiplication(listData.get(number).getPrice()+"",Exchange);
                            s2=Util.Multiplication(s1,e1);
                            tv_price2.setText("≈ ¥ "+s2);
                        }

                        else
                        {
                            s2=Util.Multiplication(listData.get(number).getPrice()+"",e1);
                            tv_price2.setText("≈ $ "+s2);
                        }
                    }


                }
            }
        });

        edit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(isclick)
                {
                    isclick=false;

                    new Handler().postDelayed(new Runnable(){
                        public void run(){
                            //execute the task
                            scrollview.scrollTo(0, DisplayUtil.getWidthPixels(CalculatorAc.this)/2);
                            isclick=true;
                        }
                    },500);
                }

                return false;
            }
        });
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.iv_change,R.id.ly_coin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_change://切换
                if(b)
                {
                    kind=2;
                    tv_sy.setText("有效算力");
                    tv_sl.setText("收益");
                    tv_price.setVisibility(View.INVISIBLE);
                    tv_price2.setVisibility(View.VISIBLE);
                    tv_d.setText(str1);
                    tv_s.setText(str2);
                    edit.setText(ss1);
                    tv_content.setText(e1);

                    if(Coin_kind==1)
                    {

                        tv_price2.setText("≈ ¥ "+s2);
                    }

                    else
                    {

                        tv_price2.setText("≈ $ "+s2);
                    }

                    b=false;
                }
                else
                {
                    kind=1;
                    tv_sy.setText("收益");
                    tv_sl.setText("有效算力");
                    tv_price.setVisibility(View.VISIBLE);
                    tv_price2.setVisibility(View.INVISIBLE);
                    tv_d.setText(str2);
                    tv_s.setText(str1);
                    edit.setText(e1);
                    tv_content.setText(ss1);
                    if(Coin_kind==1)
                    {

                        tv_price.setText("≈ ¥ "+s2);
                    }

                    else
                    {

                        tv_price.setText("≈ $ "+s2);
                    }
                    b=true;
                }
                break;
                case R.id.ly_coin:
                    if(calculatorAcPop!=null)
                    calculatorAcPop.showAsDropDown(ly_coin, 0, 0);
                    break;



        }
    }
}
